package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import sun.misc.BASE64Encoder

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat

public class apiCustomerRegist implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiCustomerRegist.class);
    private static String APPKEY;
    private static String APPID;
    private static String URL;
    private String charset = "utf-8"; //设置字符集
    private int timeout = 100000; //设置请求超时时间
    private int enableSSLflag = 0;

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        JSONObject jsonPara=new JSONObject();
        HashMap<String,Object> headMap=new HashMap<>();
        headMap.put("Content-Type","application/json");

        // 初始化返回参数
        JSONObject result = new JSONObject();

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + callJson);

        JSONObject inJson = JSONObject.fromObject(callJson);

        def appkeys=eliteBean.chaxun_sql("select parametervalue from sysc_parameter where ROLEGROUP_ID = '######' and parameter_ID='FDDKEY'","dataSource");
        Map appkeysMap =appkeys.get(0);
        APPKEY=appkeysMap.get("parametervalue");

        def appids=eliteBean.chaxun_sql("select parametervalue from sysc_parameter where ROLEGROUP_ID = '######' and parameter_ID='FDDID'","dataSource");
        Map appidsMap =appids.get(0);
        APPID=appidsMap.get("parametervalue");
        def fddurl_list=eliteBean.chaxun_sql("select target_url from sys_interface_config where type = 'fddurl' and status = '1'","dataSource");
        URL=fddurl_list[0].get("target_url");



        String bizContent=getbizContent(callJson);
        bizContent=bizContent.replaceAll("\\n","").replaceAll("\\r","");;
        JSONObject jsons=new JSONObject();
        String datetime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        jsons.put("timestamp",datetime);
        jsons.put("bizContent",bizContent);
        jsons.put("signType","SHA256");
        jsons.put("appId",APPID);
        String signString="appId="+APPID+"&bizContent="+bizContent+"&signType=SHA256&timestamp="+datetime;
        signString=signString.replace("\\n","");

        String signstr1= Util.getSHA256Str(signString).toUpperCase();
        String signstr2=Util.getSHA256Str(signstr1+APPKEY).toUpperCase();
        String sign=new BASE64Encoder().encodeBuffer(signstr2.getBytes("UTF-8"));
        jsons.put("sign",sign);
        log.debug("注册客户参数:"+jsons.toString().replace("\\n",""));
        String Assemblys=Util.doHttpPostUseHead(URL+"/account/register",jsons.toString().replace("\\n",""),charset,timeout,enableSSLflag,headMap);
        JSONObject obj = JSONObject.fromObject(Assemblys);

        return  obj.toString();
    }

    //获取bizContent
    public String getbizContent(String param){
        log.debug("param:"+param);
        String encodedObject = URLEncoder.encode(param,"UTF-8");
        String biz_contents =new BASE64Encoder().encodeBuffer(encodedObject.getBytes("UTF-8"));
        log.info("[biz_contents]:" + biz_contents);
        return biz_contents;
    }
}
